<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.mongodb.client.MongoCursor,org.bson.Document,org.bson.types.ObjectId,java.util.Date" %>
<%@ page import="mongo.Department,mongo.Major,mongo.Class" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加学生</title>
</head>
<body>
	<%
		//系专业班级
	  	Department Department = new Department();
		Major Major = new Major();
		Class Class = new Class();
	  	MongoCursor<Document> departmentList = Department.list();
		MongoCursor<Document> majorList = Major.list();
		MongoCursor<Document> classList = Class.list();
	%>
	<jsp:include page="../navbar.html" />
	<div class="container-fluid">
		<div class="row">
			<div class="col-md-1 col-md-offset-1">
				<a href="userManageServlet?method=list&type=s">
					<button type="button" class="btn btn-default"><em class="fa fa-chevron-left"></em> 返回</button>
				</a>
			</div>
		</div>
    	<div class="row mt10">
    		<form name="studentform" role="form" action="userManageServlet" method="post"  onsubmit="return check(this);" class="form-horizontal">
				<div class="col-md-5 col-md-offset-1">
					<div class="panel">
	                	<div class="panel-body">
	                  		<h4 class="page-header mt0">学生信息1</h4>
	                  	    <fieldset>
				              <div class="form-group">
				                  <label class="col-md-2 control-label">姓名</label>
				                  <div class="col-md-9">
				                     <input class="form-control" type="text" name="name" placeholder="请输入学生姓名" />
				                  </div>
				               </div> 
			                </fieldset>
		               		<fieldset>
			               		<div class="form-group">
			                  		<label class="col-md-2 control-label">学号</label>
		                  			<div class="col-md-9">
		                     			<input class="form-control" type="text" name="username" placeholder="请输入学号" />
		                  			</div>
		               			</div>	
		               		</fieldset> 
		                	<fieldset>
			               		<div class="form-group">
			               			<label class="col-md-2 control-label">性别</label>
			               			<div class="col-md-9">
			               				<label class="radio-inline c-radio">
                                 			<input id="inlineradio1" type="radio" name="sex" value="男" checked>
                                 			<span class="fa fa-circle"></span>男</label>
                        			 	<label class="radio-inline c-radio">
                           					<input id="inlineradio2" type="radio" name="sex" value="女">
                           					<span class="fa fa-circle"></span>女</label>
			               			</div>
			               		</div>	
		               		</fieldset>  
		               		<fieldset>
		               			 <div class="form-group">
			                  		<label class="col-md-2 control-label">生日</label>
		                  			<div class="col-md-9">
		                     			<input class="form-control" type="date" name="birthday" placeholder="请输入学生生日" />
		                  			</div>
		               			</div>
		               		</fieldset> 
		               		<fieldset>
			              		<div class="form-group mt15">
			                  		<label class="col-md-2 control-label">手机</label>
		                  			<div class="col-md-9">
		                     			<input class="form-control" type="text" name="phone" placeholder="请输入手机号" />
		                  			</div>
		               			</div>
		               		</fieldset>
		               		<fieldset>
			              		<div class="form-group">
			                  		<label class="col-md-2 control-label">邮箱</label>
		                  			<div class="col-md-9">
		                     			<input class="form-control" type="text" name="email" placeholder="请输入邮箱" />
		                  			</div>
		               			</div>
		               		</fieldset>           
	               		</div>
	            	</div>
	         	</div>
	         	<div class="col-md-5">
					<div class="panel">
	                	<div class="panel-body">
	                  		<h4 class="page-header mt0">学生信息2</h4>	                  	
		               		<fieldset>
			               		<div class="form-group">
			                  		<label class="col-md-2 control-label">系别</label>
		                  			<div class="col-md-9">
		                  				<select class="form-control" name="departmentid" id="selectDepart">				                  						  
		                  						<option selected>请选择系</option>
		                  						<%while(departmentList.hasNext()) {
						                  			Document document = departmentList.next();%>					                  			
						                     		<option value="<%=document.getObjectId("_id") %>"><%=document.getString("name") %></option>
						                     	<%} %> 
						                 </select>
		                  			</div>
		               			</div>	
		               		</fieldset> 
		                	<fieldset>
			               		<div class="form-group">
			                  		<label class="col-md-2 control-label">专业</label>
		                  			<div class="col-md-9">
		                  				<select class="form-control" name="majorid" id="selectMajor" placeholder="请先选择系部">
							               <option selected>请选择专业</option>					           
						                 </select>					            
		                  			</div>
		               			</div>
		               		</fieldset>  
		               		<fieldset>
		               			 <div class="form-group">
			                  		<label class="col-md-2 control-label">年级</label>
		                  			<div class="col-md-9">                			
		                     			<input class="form-control" type="text" name="grade" placeholder="请输入年级" />
		                  			</div>
		               			</div>
		               		</fieldset> 
		               		<fieldset>
			              		<div class="form-group">
			                  		<label class="col-md-2 control-label">班级</label>
		                  			<div class="col-md-9">
		                  				<select class="form-control" name="classid" id="selectClass">
		                  					 <option selected>请选择班级</option>
		                     			</select>
		                  			</div>
		               			</div>
		               		</fieldset> 
		               		<fieldset>
			              		<div class="form-group">
			                  		<label class="col-md-2 control-label">备注</label>
		                  			<div class="col-md-9">
		                     			<textarea class="form-control no-resize" name="mark" placeholder="请输入备注" rows="5"></textarea>
		                  			</div>
		               			</div>
		               		</fieldset>     
		               		<input type="hidden" name="type" value="s">  
		               		<input type="hidden" name="department" id="department">
		               		<input type="hidden" name="major" id="major">
		               		<input type="hidden" name="class" id="class">
	               		</div>
	            	</div>
	         	</div>
	         	<div class="col-md-10 col-md-offset-1 mb20">
          			<div class="col-md-6">
          				<button type="submit" class="btn btn-primary btn-block" name="method" value="add">添加</button> 
          			</div>
          			<div class="col-md-6">
          				<button type="reset" class="btn btn-default btn-block">重置</button> 
          			</div>
          		</div>
         	</form>
       </div>
    </div>
	<script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
	<script>
		//表单提交前进行数据验证，数据合法则返回true表单提交
		function check(studentform) {  
			studentform.department.value =$("#selectDepart").find("option:selected").text(); 
		    studentform.major.value =$("#selectMajor").find("option:selected").text();  
		    studentform.class.value =$("#selectClass").find("option:selected").text();  
			if(studentform.name.value=="") {  
		        swal("姓名不能为空","请重新输入","warning");  
		        return false;  
		    } 
			else if(studentform.username.value=="") {  
		        swal("学号不能为空","请重新输入","warning");  
		        return false;  
		    }  
		    else if(!isNumber(studentform.username.value)) {
		    	swal("学号不合法","请重新输入","warning");  
		        return false;  
		    }
		    else if(studentform.birthday.value=="") {  
		    	swal("生日不能为空","请重新输入","warning");   
		        return false;  
		    }   
		    else if(studentform.phone.value=="") {  
		   	    swal("手机不能为空","请重新输入","warning");   
		        return false;  
		    }
		    else if(studentform.email.value=="") {  
		   	    swal("邮箱不能为空","请重新输入","warning");   
		        return false;  
		    }
		    else if(studentform.department.value=="请选择系") {
				swal("系部不能为空","请重新选择","warning");
				return false;  
			}
			else if(studentform.major.value=="请选择专业" || studentform.major.value=="目前该系没有专业") {
				swal("专业不能为空","请重新选择","warning");
				return false;  
			}
			else if(studentform.grade.value=="") {  
			   	swal("年级不能为空","请重新输入","warning");   
			    return false;  
			}
			else if(studentform.class.value=="请选择班级" || studentform.class.value=="目前该专业没有班级") {
				swal("班级不能为空","请重新选择","warning");
				return false;  
			}
	
		    return true;  
		}   
		  
		$(document).ready(function(){
	    	//系部下拉选择框变化
	    	$("#selectDepart").change(function(){
	    		var checkValue=$("#selectDepart").val();  
	    		$.ajax({
	                 type: "post",
	                 url: "../../common/profileServlet?method=getmajor&_id="+checkValue,
	                 dataType: "text",
	                 success: function(data){
	                	 var result = JSON.parse(data); 
	                	 $("#selectMajor").empty(); //empty()方法删除被选元素的子元素（option）
	                	 $("#selectClass").empty();                
	                	 $("#selectMajor").append("<option selected>请选择专业</option>");
	                	 $("#selectClass").append("<option selected>请选择班级</option>");
	                	 if(result.length<1) {
	                		 $("#selectMajor").append("<option>目前该系没有专业</option>");
	                	 }
	                	 for(var i=0; i<result.length;i++){              
	                         $("#selectMajor").append("<option value='"+result[i]._id+"'>"+result[i].name+"</option>");
	                	 }
	                	 //$('#selectMajor option:first').attr('selected','selected');
	                     console.log(result);//修改id为showMsg标签的html
	                 }, error: function(){
	                	 swal("请求出错","请重新选择","warning");
	                 }
	           })
	    	});
	    	
	    	//专业下拉选择框变化
	    	$("#selectMajor").change(function(){  
	    		var checkValue=$("#selectMajor").val();  
	    		$.ajax({
	                 type: "post",
	                 url: "../../common/profileServlet?method=getclass&_id="+checkValue,
	                 dataType: "text",
	                 success: function(data){
	                	 var result = JSON.parse(data); 
	                	 $("#selectClass").empty(); //empty()方法删除被选元素的子元素（option）
	                	 if(result.length<1) {
	                		 $("#selectClass").append("<option>目前该专业没有班级</option>");
	                	 }
	                	 for(var i=0; i<result.length;i++){              
	                         $("#selectClass").append("<option value='"+result[i]._id+"'>"+result[i].name+"</option>");
	                	 }
	                     console.log("class:"+result);//修改id为showMsg标签的html
	                 }, error: function(){
	                	 swal("请求出错","请重新选择","warning");
	                 }
	           })
	    	});
	  	});
		
		function isNumber(str)          // 判断是否为非负整数  
		{  
		    var rx = /^[0-9]+$/;  
		    return rx.test(str);  
		} 
	</script>
</body>
</html>